#
# 构建 mysql 5.6.27
#

#https://dev.mysql.com/get/Downloads/MySQL-5.6/MySQL-server-5.6.39-1.el6.x86_64.rpm

FROM debian:jessie
MAINTAINER joen kai168@126.com

ENV LANG C.UTF-8
ENV MYSQL_VERSION 5.6.39

RUN set -ex; \
			{ \
				echo 'deb http://mirrors.aliyun.com/debian/ jessie main non-free contrib'; \
				echo 'deb http://mirrors.aliyun.com/debian/ jessie-proposed-updates main non-free contrib'; \
				echo 'deb-src http://mirrors.aliyun.com/debian/ jessie main non-free contrib'; \
				echo 'deb-src http://mirrors.aliyun.com/debian/ jessie-proposed-updates main non-free contrib'; \
			} | tee /etc/apt/sources.list

# RUN apt-get update && apt-get install -y --no-install-recommends gnupg dirmngr && rm -rf /var/lib/apt/lists/*

# add gosu for easy step-down from root
ENV GOSU_VERSION 1.10
RUN set -ex; \
	\
	fetchDeps=' \
		ca-certificates \
		wget \
	'; \
	apt-get update; \
	apt-get install -y --no-install-recommends $fetchDeps; \
	rm -rf /var/lib/apt/lists/*; \
	\
	dpkgArch="$(dpkg --print-architecture | awk -F- '{ print $NF }')"; \
	wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$dpkgArch"; \
	wget -O /usr/local/bin/gosu.asc "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$dpkgArch.asc"; \
	\
# verify the signature
	export GNUPGHOME="$(mktemp -d)"; \
	gpg --keyserver ha.pool.sks-keyservers.net --recv-keys B42F6819007F00F88E364FD4036A9C25BF357DD4; \
	gpg --batch --verify /usr/local/bin/gosu.asc /usr/local/bin/gosu; \
	rm -r "$GNUPGHOME" /usr/local/bin/gosu.asc; \
	\
	chmod +x /usr/local/bin/gosu; \
# verify that the binary works
	gosu nobody true; \
	\
	apt-get purge -y --auto-remove $fetchDeps

RUN groupadd -g 2001 mysql \
      && useradd -g 2001 -u 2001 -s /sbin/nologin mysql \
      && mkdir -p /opt/mysql/run /opt/mysql/data /opt/soft /opt/mysql/logs\
			&& chmod 777 /opt/soft \
      && chown mysql.mysql -R /opt/mysql 
# COPY software/mysql-5.6.27.tar.gz /opt/soft/

RUN set -ex; \
			apt-get update; \
			apt-get install -y --no-install-recommends wget ca-certificates; \
			cd /opt/soft; \
			gosu mysql wget https://github.com/mysql/mysql-server/archive/mysql-$MYSQL_VERSION.tar.gz; \
			gosu mysql tar xzf mysql-$MYSQL_VERSION.tar.gz; \
			buildDeps='wget ca-certificates bison cmake make g++ libncurses5-dev libaio-dev '; \
			apt-get update; \
			apt-get install -y --no-install-recommends $buildDeps; \
			rm -rf /var/lib/apt/lists/*; \
			cd /opt/soft; \
			cd mysql-server-mysql-$MYSQL_VERSION; \
			gosu mysql cmake \
				-DCMAKE_INSTALL_PREFIX=/opt/mysql/run \
				-DMYSQL_DATADIR=/opt/mysql/data \
				-DSYSCONFDIR=/opt/mysql/run/etc \
				-DBUILD_CONFIG=mysql_release \
				-DENABLED_PROFILING=0 \
				-DWITH_EMBEDDED_SERVER=0 \
				-DWITH_UNIT_TESTS=0 \
				-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
				-DENABLED_LOCAL_INFILE=1 \
				-DWITH_INNOBASE_STORAGE_ENGINE=1 \
				-DWITH_PARTITION_STORAGE_ENGINE=1 \
				-DEXTRA_CHARSETS=all \
				-DDEFAULT_CHARSET=utf8 \
				-DDEFAULT_COLLATION=utf8_general_ci .; \
			cd /opt/soft; \
			cd mysql-server-mysql-$MYSQL_VERSION; \
			gosu mysql make -j$(getconf _NPROCESSORS_ONLN); \
			gosu mysql make install; \
			rm -rf /opt/soft/*; \
			rm -rf /opt/mysql/run/mysql-test; \
			apt-get purge -y --auto-remove $buildDeps;

RUN set -ex; \
			apt-get update && apt-get install -y --no-install-recommends pwgen perl libaio-dev; \
			rm -rf /var/lib/apt/lists/*; 	

RUN set -ex; \
			gosu mysql mkdir -p /opt/mysql/run/etc; \
			ln -s /opt/mysql/run/bin/* /usr/bin; \
			{ \
				echo '[mysqld]'; \
				echo 'skip-host-cache'; \
				echo 'skip-name-resolve'; \
				echo 'log-error=/opt/mysql/logs/mysql.log'; \
				echo 'slow_query_log=1'; \
				echo 'slow_query_log_file=/opt/mysql/logs/slow.log'; \
				echo 'character-set-client-handshake=FALSE'; \
				echo 'character-set-server=utf8'; \
				echo 'collation-server=utf8_general_ci'; \
				# echo 'init-connect=\\\'SET NAMES utf8\\\''; \
				echo '[mysqld_safe]'; \
				echo 'default-character-set=utf8'; \
				echo '[client]'; \
				echo 'default-character-set=utf8'; \
				echo '[mysql]'; \
				echo 'default-character-set=utf8'; \
			}|tee /opt/mysql/run/etc/my.cnf; \
			chown mysql.mysql /opt/mysql/run/etc/my.cnf; 

COPY dockers/mysql/5.6/docker-entrypoint.sh  /opt/mysql/
RUN chmod +x /opt/mysql/docker-entrypoint.sh
ENTRYPOINT ["/opt/mysql/docker-entrypoint.sh"]

EXPOSE 3306

CMD ["/opt/mysql/run/bin/mysqld","--user=mysql"]



